/**
 * @author Samuel Gonzalez
 * @version 1.0.6 Thu May 10, 2012 10:04PM GMT-400
 */

/*
 * Copyright (C) 2011  Samuel Gonzalez, PR
 * BenchGraph. Generates graphics and HTML tables based on HTTPERF output.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * In addition, as a special exception, the copyright holders give permission
 * to link the code of this work with the OpenSSL project's "OpenSSL" library
 * (or with modified versions of it that use the same license as the "OpenSSL" 
 * library), and distribute linked combinations including the two.  You must
 * obey the GNU General Public License in all respects for all of the code
 * used other than "OpenSSL".  If you modify this file, you may extend this
 * exception to your version of the file, but you are not obligated to do so.
 * If you do not wish to do so, delete this exception statement from your
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 
 */

import java.util.ArrayList;

public class Tables {

	private final String TABLE = "EMPTY";
	
	// Attribute
	private String table; // Hold entire table
	
	/**
	 * Initialize attributes to default values
	 */
	public Tables(){
		this.table = this.TABLE;
	}
	
	/**
	 * Initialize attributes to received values.
	 * @param data
	 */
	public Tables(ArrayList<String> data){
		createTable(data);
	}
	
	/**
	 * Return table
	 * @return
	 */
	public String getTable(){ // Getter
		return this.table;
	}
	
	/**
	 * Set table
	 * @param table
	 */
	public void setTable(String table){ // Setter
		this.table = (table != null)? table : this.TABLE;
	}
	
	/**
	 * Create table string using data read
	 * @param data
	 */
	private void createTable(ArrayList<String> data){
		String table = "\t\t\t\t<table style=\"margin: 0px auto 0px auto;\" BORDER=\"8\" CELLSPACING=\"1\" CELLPADDING=\"1\">";
		if(data.size() % 2 != 0){//!isNumeric(data.get(1))){
			table += "<tr><th style=\"background-color: orange;\" colspan=\"2\">" + data.get(0) + "</th><tr>";
			for(int i = 1; i < data.size(); i+=2){
				table += "<tr><td>" + data.get(i) + "</td><td>" + data.get(i + 1) + "</td></tr>";
			}
		}
		else if(data.size() % 2 == 0){
			for(int i = 0; i < data.size(); i+=2){
				table += "<tr><th style=\"background-color: orange;\">" + data.get(i) + "</th></tr><tr><td>" + data.get(i + 1) + "</td></tr>";
			}
		}
		table += "</table>";
		
		setTable(table);

	}	
}

